package com.yuqian.JavaSe.thread.cap1.title9;
/**
 * 暂停线程
 * 1.suspend() 暂停线程
 * 2.resume() 恢复线程
 * @author yuqian
 * 
 * 缺点：
 * 使用suspend会独占锁
 * 会造成数据不同步
 *
 *
 *yield() 方法放弃cpu时间片然后去竞争
 *
 */
public class PauseThread {
	public static void main(String[] args) throws InterruptedException {
		/*MyThread my  = new MyThread();
		my.start();
		Thread.sleep(2000);
		System.out.println("1. i:"+my.getI());
		my.suspend();
		System.out.println("2-1. i:"+my.getI());
		Thread.sleep(2000);
		System.out.println("2-2. i:"+my.getI());
		my.resume();
		Thread.sleep(2000);
		System.out.println("3. i:"+my.getI());*/
		
		//测试suspend造成的数据不同步
		final MyUser mu=new MyUser();
		Thread th = new Thread(){
			@Override
			public void run(){
				mu.show("a", "aaa");
			}
		};
		th.start();
		Thread.sleep(5000);
		System.out.println(mu.name+"  "+mu.password);
	}
}

class MyThread extends Thread{
	private int i=0;
	
	public int getI(){
		return i;
	}
	@Override
	public void run() {
	
		while(true){
			i++;
		}
	}
}

class MyUser{
	String name = "1";
	String password="11";
	
	public void show(String mc,String mm){
		name =mc;
		if("a".equals(mc)){
			Thread.currentThread().suspend();
		}
		password =mm;
	}
}
